﻿using System;
using System.Collections.Generic;
using NOSDC.Repositories;
using NOSDC.Pocos;
using NOSDC.Services.Extensions;

namespace NOSDC.Services
{
    public sealed class EventService : IEventService
    {
        private readonly IEventsRepository _repository;

        public EventService(IEventsRepository repository)
        {
            if (repository == null)
                throw new ArgumentException("You must provide a repository");

            _repository = repository;
        }

        public EventService() : this(new EventsRepository()) { }

        public IEnumerable<Events> GetAllEvents()
        {
            return _repository.GetAllEvents();
        }

        public Events GetUpcomingEvent()
        {
            return _repository.GetUpcomingEvent();
        }
        public IEnumerable<Events> GetAllUpcomingEvents()
        {
            return _repository.GetAllUpcomingEvents();
        }

        public Events GetUpcomingEventWidget()
        {
            Events e = _repository.GetUpcomingEvent();

            if (e != null) 
            {
                e.Description = e.Description.Truncate(250);
                e.Description = e.Description.StripHTML();
            }

            return e;
        }

        public bool CreateEvent(Events e)
        {
            return _repository.CreateEvent(e) == 1;
        }

        public bool CreateEvent(Events e, List<int> sponsorids, List<int> speakerids)
        {
            return _repository.CreateEvent(e, sponsorids, speakerids) == 1;
        }

        public bool UpdateEvent(Events e)
        {
            return _repository.UpdateEvent(e) == 1;
        }

        public bool UpdateEvent(Events e, List<int> sponsorids, List<int> speakerids)
        {
            return _repository.UpdateEvent(e, sponsorids, speakerids) == 1;
        }

        public Events GetEventByID(int id)
        {
            return _repository.GetEventByID(id);
        }

        public bool AttendEvent(Guid userid, int eventId)
        {
            var e = _repository.GetEventByID(eventId);

            if (e.StartDate < DateTime.Now) // prevent people from registering in the past
            {
                throw new InvalidOperationException("Event has already completed");
            }

            return _repository.AttendEvent(userid, eventId);
        }

        public bool IsAttendingEvent(int eventID, Guid userID)
        {
            return _repository.IsAttendingEvent(eventID, userID);
        }

        public IList<EventAttendee> GetAttendees(int eventId)
        {
            return _repository.GetAttendees(eventId);
        }

        public IList<FinalEventAttendee> GetFinalAttendees(int eventId)
        {
            return _repository.GetFinalAttendees(eventId);
        }

        public IEnumerable<Events> GetPreviousEvents(int previousEvents)
        {
            return _repository.GetPreviousEvents(previousEvents);
        }

        public void RemindMembersOfEvent(int eventId)
        {

        }

        public void FinalizeEvent(IList<FinalEventAttendee> attendees, int eventId)
        {
            _repository.FinalizeEvent(attendees, eventId);
        }

        public IList<Events> GetEventsByPage(int page, int pageSize)
        {
            return _repository.GetEventsByPage(page, pageSize);
        }

        public int GetTotalEvents()
        {
            return _repository.GeTotalEvents();
        }
    }
}
